﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using YoUI.Core;
using LitJson;
namespace YoUI.Core
{
    public class YoConfigMgr
    {
        // 所有配置路径
        public Dictionary<string, string> _DicCFG = new Dictionary<string, string>();
        // UI窗口的路径
        public Dictionary<string, string> _DicUICFG = new Dictionary<string, string>();
        // UI音效路径
        public Dictionary<string, string> _DicAudioCFG = new Dictionary<string, string>();

        private static YoConfigMgr _YoConfigMgr;

        public static YoConfigMgr _
        {
            get
            {
                if(_YoConfigMgr == null)
                {
                    _YoConfigMgr = new YoConfigMgr();
                }
                return _YoConfigMgr;
            }
        }

        public YoConfigMgr()
        {
            // 加载所有配置文件
            LoadCFG(ref _DicCFG, YoSysDefine.Yo_CFG);


            string path = "";
            // 加载UI配置
            if(_DicCFG.TryGetValue(YoSysDefine.Yo_CFG_UI,out path))
            {
                LoadCFG(ref _DicUICFG, path);
            }

            // 加载音频配置
            if (_DicCFG.TryGetValue(YoSysDefine.Yo_CFG_Audio, out path))
            {
                LoadCFG(ref _DicAudioCFG, path);
            }
        }

        /// <summary>
        /// 加载一般配置
        /// </summary>
        /// <returns></returns>
        public void LoadCFG(ref Dictionary<string,string> dicPath,string path)
        {
            // 拿到json
            string json = Resources.Load<TextAsset>(YoSysDefine.Yo_CFG_ROOT + "/" + path).text;

            JsonData data = JsonMapper.ToObject(json);

            foreach (var item in data.Keys)
            {
                dicPath.Add(item, data[item].ToString());
            }
        }


    }
}

